home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17081 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help: Bitwise operators
  5. Date: Sat, 13 Apr 1996 16:32:10 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4koksu$fai@news.halcyon.com>
  8. References: <4knb73$f15@wumpus.its.uow.edu.au>
  9. NNTP-Posting-Host: blv-pm3-ip22.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. sjl01@wumpus.its.uow.edu.au (Stuart John Langley) wrote:
  13.  
  14. >Hi All,
  15.  
  16. >I need some help understanding some code that i need to translate.
  17. >I have the following,
  18.  
  19. >unsigned char byte;
  20.  
  21. >then either - 
  22.  
  23. >byte = (byte >> 1) | 0;
  24.  
  25. >or 
  26.  
  27. >byte = (byte >> 1) | 128;
  28.  
  29. >I know that the first case places a 0 in the left most bit of byte,
  30. >and moves everything else to the right, while the second places a 1
  31. >in the leftmost bit. However, when I look at the texts and hepfiles,
  32. >this seems to indicate thet the 0 or 1 should be placed in the rightmost
  33. >bit and everything else moved left. Can somebody please help me out with
  34. >this problem>
  35.  
  36. >Thanks,
  37.  
  38. >Stuart
  39.  
  40. What exactly do the docs say?  What is happening is the byte is
  41. right-shifted by one, and that result bitwise-ORd with the given
  42. value.
  43.  
  44. In the first case the bitwise-OR of zero does absolutely nothing.  
  45. The shift itself made the zero in the most-significant (leftmost) bit.
  46. (This only holds true if byte is of type unsigned char; for normal,
  47. signed char types, shifting preserves the sign-bit, the leftmost bit.
  48. In this way, shifting right by 1 is always equivalent to / 2).
  49.  
  50. In the second case, the | 128 is really | 0x80, thus setting the
  51. leftmost bit after the shift.
  52.  
  53.                         --Norm 
  54.  
  55.